﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Utility;

namespace YouHP.Pages.User
{
    public class Password : YouHP.Pages.User.UserBasePage
    {

        /// <summary>
        /// 当前用户信息
        /// </summary>
        public UserInfo user = new UserInfo();

        protected override void ShowPage()
        {
            base.ShowPage();
            
            this.AddLocation("修改密码", "/User/Password.aspx");
            this.SetTitle("修改密码");

            if (UserData.Id == -1)
            {
                AddErrLine("你尚未登录");
                return;
            }
            user = UserBLL.Instance.GetUserInfo(UserData.Id);

            if (RequestTools.IsPost())
            {
                string oldpassword = RequestTools.GetString("oldpassword");
                string newpassword = RequestTools.GetString("newpassword");
                string newpassword2 = RequestTools.GetString("newpassword2");

                string password = Utils.MD5(oldpassword, 16);
                UserInfo user1 = UserBLL.Instance.CheckUserLogin(user.UserName, password);

                if (user1 ==null)
                {
                    AddErrLine("你的原密码错误");
                }
                if (newpassword != newpassword2)
                {
                    AddErrLine("新密码两次输入不一致");
                }

                if (newpassword.Equals(""))
                {
                    newpassword = oldpassword;
                }

                if (newpassword.Length < 6)
                {
                    AddErrLine("密码不得少于6个字符");
                }

                if (IsErr())
                {
                    return;
                }
                else
                {

                    user.Password = Utils.MD5(newpassword,16);
                    bool flag = UserBLL.Instance.SendPasswordNotification(user, newpassword);
                    if (flag)
                    {
                        UserBLL.Instance.UpdateUserPassword(user);
                    }                    

                    SetUrl("/User/Password.aspx");
                    SetMetaRefresh();
                    SetShowBackLink(true);
                    AddMsgLine("修改密码完毕, 同时已经更新了您的登录信息");
                }
            }
        }
    }
}
